﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;



/// <summary>
/// 线列阵，以线列第一排中央作为锚点
/// </summary>


namespace Formation
{
    public class LineFormation : FormationBase
    {
        public int lineWidth; //阵列宽度，即每一行的单位个数
        public float verticalInterval; //垂直方向空隙
        public float horizantalInterval; //水平方向空隙
        

        public LineFormation(int lineWidth, float verticalInterval, float horizantalInterval,float arriveDistance)
        {
            this.lineWidth = lineWidth;
            this.verticalInterval = verticalInterval;
            this.horizantalInterval = horizantalInterval;
            this.ArriveDistance = arriveDistance;
        }


        public override float FormationDistance(FormationUnit unit, FormationSlot slot)
        {
            float dis = 0;

            dis = unit.unit.transform.position.SqrDistance(slot.position);
            if (unit.tag == "cannon")
            {
                if (slot.id > (slotCount * 0.75 - 1))
                {
                    dis *= 0.5f;
                }
                else if (slot.id < slotCount * 0.25)
                {
                    dis *= 5f;
                    dis = float.MaxValue;

                }
                else
                {
                    dis *= 2f;
                    dis = float.MaxValue;
                }
            }
            return dis;
        }


        public override List<FormationSlot> GenerateSlotList(Transform pivot, List<FormationUnit> units)
        {

            int count = units.Count;
            List<FormationSlot> slots = new List<FormationSlot>();
            Quaternion rot = pivot.transform.rotation;
            NavMeshHit MeshHit;

            Vector3 RightInterVec = pivot.right * horizantalInterval;
            Vector3 ForwardInterVec = pivot.forward * verticalInterval;
            for (int y = 0; y < count / lineWidth + 1; y++)
            {
                for (int x = 0; x < lineWidth; x++)
                {
                    Vector3 cur = GetSlotPos(x,y,pivot.position, RightInterVec, pivot.forward);
                    if (y * lineWidth + x >= count) break;
                    if (NavMesh.SamplePosition(cur, out MeshHit, 10, 1))
                    {
                        cur = MeshHit.position;
                    }
                    else
                    {
                        Debug.Log("找不到地形位置");
                    }

                    Quaternion slotForward =rot;
                    FormationSlot slot = new FormationSlot(cur, slotForward, y * lineWidth + x);
                    slot.tag = "lineUnit";
                    slots.Add(slot);
                }
            }
            return slots;
        }

        //public override void UpdateSlotList(Transform pivot)
        //{
        //    UpdateLineFormationSlotPosAndRot(pivot);
        //    UpdateUnitTarget();
        //    UpdateFightRatio();
        //}

        public override void UpdateLineFormationSlotPosAndRot(Transform pivot)
        {
            Vector3 RightInterVec = pivot.right* horizantalInterval; 
            Vector3 ForwardInterVec = pivot.forward* verticalInterval;
            Vector3 centerPos = pivot.position - ((float)(lineWidth - 1) / 2) * RightInterVec;
            Quaternion pivotRotation = pivot.transform.rotation;
            foreach (var fu in unitSlotList)
            {
                if (fu.Value.unit == null) continue;
                Vector3 newPos = GetSlotPos(fu.Key.id,centerPos, RightInterVec, ForwardInterVec);
                fu.Key.position = newPos;
                fu.Key.rotation = pivotRotation;
            }
        }

        


        private Vector3 GetSlotPos(int id,Vector3 centerPos,Vector3 RightInterVec,Vector3 ForwardInterVec)
        {
            int x = id % lineWidth;
            int y = id / lineWidth;
            return GetSlotPos(x, y, centerPos, RightInterVec, ForwardInterVec);
            
        }
        private Vector3 GetSlotPos(int x,int y, Vector3 centerPos, Vector3 RightInterVec, Vector3 ForwardInterVec)
        {
            Vector3 planeSlotPos = centerPos
                    + (RightInterVec * x)
                    + (-ForwardInterVec * y);
            NavMeshHit hit;
            NavMesh.SamplePosition(planeSlotPos, out hit, navMeshSampleRadius, navMeshAreaMask);
            if (!hit.hit) throw (new Exception("阵型槽没有找到最近的导航网格"));
            return hit.position;

        }
    }
}

